package kz.edu.sdu.atm.ui.client;

import java.math.BigDecimal;

import kz.edu.sdu.atm.client.Client;
import kz.edu.sdu.atm.models.UserDTO;

import com.trolltech.qt.gui.QGridLayout;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QWidget;

public class MainMenu extends QMyWidget {
	UserDTO user;

	QPushButton logout;
	QPushButton balance;
	QPushButton withdraw;
	QPushButton deposit;
	QPushButton send;
	QWidget p;
	Form form;

	public MainMenu(QWidget parent, Client client, UserDTO user) {
		super(null, client);
		p = parent;
		this.user = user;
	}

	@Override
	void init() {
		form = new Form(this, client);
		setFixedSize(400, 600);

		logout = new QPushButton("Logout");
		balance = new QPushButton("Check Balance");
		withdraw = new QPushButton("Money withdrawal");
		deposit = new QPushButton("Deposit");
		send = new QPushButton("Transfer");

		layout = new QGridLayout();
		layout.addWidget(balance);
		layout.addWidget(withdraw);
		layout.addWidget(deposit);
		layout.addWidget(send);
		layout.addWidget(logout);
		setLayout(layout);
	}

	@Override
	void connectAll() {
		logout.clicked.connect(this, "logout()");
		balance.clicked.connect(this, "balance()");

		deposit.clicked.connect(this, "hide()");
		deposit.clicked.connect(form, "deposit()");
		deposit.clicked.connect(form, "show()");

		withdraw.clicked.connect(this, "hide()");
		withdraw.clicked.connect(form, "withdraw()");
		withdraw.clicked.connect(form, "show()");

		send.clicked.connect(this, "hide()");
		send.clicked.connect(form, "send()");
		send.clicked.connect(form, "show()");

		form.deposit.connect(this, "deposit()");
		form.withdraw.connect(this, "withdraw()");
		form.send.connect(this, "send()");
	}


	// slots
	public void balance() {
		try {
			BigDecimal result = client.balance(user.getUsername(), user
					.getPassword());
			System.out.println(String.format("Your current balance is %f KZT",
					result));
			showMessage(String.format("Your current balance is %f KZT", result));
		} catch (Exception e) {
			e.printStackTrace();
			showMessage("Check balance end up with error!");
		}
	}

	public void deposit() {
		try {
			BigDecimal result = client.deposit(user.getUsername(), user
					.getPassword(), new BigDecimal(form.amount.text()));
			showMessage(String.format(
					"Success! Your current balance is %f KZT",
					result));
		} catch (Exception e) {
			e.printStackTrace();
			showMessage("Deposit end up with error!");
		}
	}

	public void withdraw() {
		try {
			BigDecimal result = client.withdraw(user.getUsername(), user
					.getPassword(), new BigDecimal(form.amount.text()));
			showMessage(String.format(
					"Success! Your current balance is %f KZT", result));
		} catch (Exception e) {
			e.printStackTrace();
			showMessage("Withdrawal end up with error!");
		}
	}

	public void send() {
		try {
			BigDecimal result = client.send(user.getUsername(), user
					.getPassword(), form.to.text(), new BigDecimal(form.amount
					.text()));
			showMessage(String.format(
					"Success! Your current balance is %f KZT", result));
		} catch (Exception e) {
			e.printStackTrace();
			showMessage("Send end up with error!");
		}
	}

	public void logout() {
		hide();
		p.show();
	}

}
